const express = require('express');
const router = express.Router();
const createError = require('http-errors');
const {query} = require('../models/db');
const {togame} = require('../models');


router.get('/:channel', async (req, res) => {

    const showsql = 'SELECT * FROM kaikeba.toshow';
    const tvsql = 'SELECT * FROM kaikeba.totv';

    const title = getTitle(res, req.params.channel);
    const showCo = await query(showsql);
    const tvCo = await query(tvsql);
    if (title) {
        res.render('channel/' + req.params.channel, {title, show: showCo, tv: tvCo});
    } else {
        res.redirect('/channel/index');
    }
});



//获取标题
function getTitle(res, channel) {
    for (const c of res.locals.channel) {
        if (c.url.indexOf(channel) != -1) {
            return c.name;
        }
    }
    return '';
}

module.exports = router;